package com.codes.service.impl;

import com.codes.core.common.enumtype.wordtpl.*;
import com.codes.core.utils.word.PoiTlUtils;
import com.codes.dao.CaseDocumentMapper;
import com.codes.service.CaseDocumentSV;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.util.BytePictureUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 
 * @Date 2020/7/6 11:41
 * @Created  peanut
 */
@Slf4j
@Service
public class CaseDocumentImpl implements CaseDocumentSV {

    @Autowired
    private CaseDocumentMapper caseDocumentMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return caseDocumentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(Map record) {
        return caseDocumentMapper.insertSelective(record);
    }

    @Override
    public Map<String,Object> selectByPrimaryKey(Integer id) {
        return caseDocumentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Map record) {
        return caseDocumentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<Map<String, Object>> getList(Map map) {
        return caseDocumentMapper.getList(map);
    }

    @Override
    public int insertCaseDoc(Integer caseId, Integer tableId, CaseTypeEnum caseTypeEnum, TplTableMappingEnum tplTableMappingEnum, TplPersonTypeEnum tplPersonTypeEnum) {
        // 案件类型 1载质量2几何3年检4动态监控5改装6.环保7线路',
        Map<String,Object>  reMap = new HashMap(10);
        Integer caseType = caseTypeEnum.getCode(); // 案件类型
        Integer tableType = tplTableMappingEnum.getCode(); // 对应的表 对应的 模板
        Integer tplDetailType = tplPersonTypeEnum.getCode();    // 对应的详细模板类型   如 立案登记表个人  立案登记表企业
        // 获取模板信息
        reMap = getTpl(caseType,tableType,tplDetailType);
        reMap.put("caseId",caseId);
        reMap.put("tableType",tableType);
        reMap.put("tableId",tableId);
        reMap.put("caseType",caseType);


        return caseDocumentMapper.insertSelective(reMap);
    }

    // 获取 模板并 渲染
    @Override
    public File getDoc(Integer caseDocumentId) throws IOException {
        //  文书 id
        // 获取文书表详情
        Map<String,Object> caseDocMap = caseDocumentMapper.selectByPrimaryKey(caseDocumentId);
        /**
         * "tableType": 1,
         *       "templateName": "站点信息检测表.docx",
         *       "caseDocumentId": 4,
         *       "caseId": 1,
         *       "tableId": "1",
         *       "templateId": 1005,
         *       "caseType": 1
         */
        File file = null;
        Integer caseType = MapUtils.getInteger(caseDocMap,"caseType");
        Integer tableType = MapUtils.getInteger(caseDocMap,"tableType");
        Integer templateId = MapUtils.getInteger(caseDocMap,"templateId");
        Integer tableId = MapUtils.getInteger(caseDocMap,"tableId");
        if(TplTableMappingEnum.zhandianxinxi.getCode() == tableType){
            // 站点信息监测表
            file = zhandian(caseType,templateId,tableId);
        }
        if(TplTableMappingEnum.liandengji.getCode() == tableType){
            // 立案登记表

        }

        //  表的 类型  如 站点信息  立案登记   询问记录
       // Integer tableType = TplTableMappingEnum.zhandianxinxi.getCode();



        // 载质量 超限  获取 载质量超限的 模板 文件夹
        //  根据模板id 获取到模板  加载


        return file;
    }

    /****
     * @param caseType  案件类型 如  载质量  几何
     * @param templateId    文书表的 模板ID
     * @param tableId   文书对应的 关联的 主键ID
     * @Description   站点信息监测表文书渲染        根据模板id 获取模板, 根据关联表 id 获取模板对应的表的数据  然后渲染模板
     * @return java.io.File    返回 渲染后的 word 文书
     * @Date 2020/7/6 17:21
     * @Created by peanut
     */
    private File zhandian(Integer caseType, Integer templateId, Integer tableId) throws IOException {
        File file = null;
                String tplUrl = TplUrlEnum.getLabelByCode(caseType);
        String tplName = TplWordEnum.getLabelByCode(templateId);
        Map<String,Object> reMap = new HashMap<>(30); // 后台获取的 站点信息表详情 map =  zhandian service  .getById();
        String chepai = "豫A 88888"; // MapUtils.getString(reMap,"chepai");
        String carType = "大货车";
        String yunshuzheng = "运输证001";
        String xukezheng = "经营许可证000001";
        String zhoushu = "8轴";
        String xianzhong = "限重 60吨";
        String waikuo = "长:6666mm,宽:7777mm,高:8888mm";
        String xiacinianshen =  "2021年10月1日";
        String chucifazheng = "2008年10月1日",
        youxiaoqi = "2009年10月1日",
                userName = "小花生"//公民姓名
        ,sex = "男",age = "9",address1 = "河南省新乡市友谊路新二街",address2 = "河南省新乡市友谊路新一街"
                ,idCard = "410123199912341234",phone1 = "18312341234",phone2 = "17321341234"
                ,farenName ="法师001",fadingdaibiao="刘海珠"
                ;

        Map<String,Object> tplMap = new HashMap<String, Object>(){{

            put("chepai", chepai);            put("carType", carType);
            put("yunshuzheng", yunshuzheng);            put("xukezheng", xukezheng);
            put("zhoushu", zhoushu);            put("xianzhong", xianzhong);
            put("waikuo", waikuo);            put("xiacinianshen", xiacinianshen);
            put("chucifazheng", chucifazheng);            put("youxiaoqi", youxiaoqi);
            put("userName", userName);            put("sex", sex);            put("age", age);
            put("address1", address1);            put("address2", address2);

            put("idCard", idCard);            put("farenName", farenName);
            put("fadingdaibiao", fadingdaibiao);
            put("phone1", phone1);            put("phone2", phone2);
            // 本地图片                                   宽           高           uri
            put("image1", new PictureRenderData(600, 250, "D:\\dev_data\\images\\111.jpg"));
            put("image2", new PictureRenderData(600, 250, "D:\\dev_data\\images\\333.jpg"));
            // 图片流
//            put("localbyte", new PictureRenderData(80, 100, ".png"
//                    , new FileInputStream("./logo.png")));
            // 网络图片
//            put("urlpicture", new PictureRenderData(50, 50, ".png"
//                    , BytePictureUtils.getUrlBufferedImage("http://deepoove.com/images/icecream.png")));

        }};


        file = PoiTlUtils.getWordDoc(tplUrl,tplName,tplMap);



        return file;
    }



    // 根据案件类型  对应的表模板类型   详情模板类型   返回 对应的 文书信息
    private Map<String,Object> getTpl( Integer caseType, Integer tableType,Integer tplDetailType){
        Map<String,Object> reMap = new HashMap<>(10);
        TplWordEnum re = null; //  对应的文书模板详细信息
        boolean geren = tplDetailType == TplPersonTypeEnum.geren.getCode();
        switch (caseType){
            case 1  : //载质量
                log.info("载质量");
                switch (tableType){
                    case 1:  //    zhandianxinxi(1,"站点信息监测表"),
                        if(tplDetailType == TplPersonTypeEnum.zhedanghaopai.getCode()){
                            re = TplWordEnum.zaizhiliang6;// 站点信息监测表遮挡号牌
                        }else {
                            re = TplWordEnum.zaizhiliang5;// 站点信息监测表
                        }
                        break;
                    case 2:      //    liandengji(2,"立案登记表"),
                        if(geren){ // 个人
                            re = TplWordEnum.zaizhiliang3;
                        }else {
                            re = TplWordEnum.zaizhiliang4;  // 企业
                        }
                        break;
                    case 3:   //    chuliyijian(3,"案件处理意见"),
                        if(geren){
                            re = TplWordEnum.zaizhiliang1;
                        }else {
                            re = TplWordEnum.zaizhiliang2;
                        }
                        break;
                    case 4:     //    xunwenbilu(4,"询问笔录"),
                        if(tplDetailType == TplPersonTypeEnum.chezhufaren.getCode()) re = TplWordEnum.zaizhiliang10;
                        if(tplDetailType == TplPersonTypeEnum.daliren.getCode()) re = TplWordEnum.zaizhiliang09;
                        if(tplDetailType == TplPersonTypeEnum.jiahsiyuan.getCode()) re = TplWordEnum.zaizhiliang11;
                        break;
                    case 5:     //    weifaxingwei(5,"违法行为通知"),
                        re = TplWordEnum.zaizhiliang12;
                        break;
                    case 6:   //    songdahuizheng(6,"送达回证"),
                        re = TplWordEnum.zaizhiliang13;
                        break;
                    case 7:   //    chenshu(7,"陈述申辩"),
                        re = TplWordEnum.zaizhiliang14;
                        break;
                    case 8:   //    jieanbaogao(8,"结案报告"),
                        if(geren) re = TplWordEnum.zaizhiliang7;
                        if(!geren) re = TplWordEnum.zaizhiliang8;
                        break;
                    default:
                        break;
                }
                break;
            case 2 :   // 案件类型 1载质量2几何3年检4动态监控5改装6.环保7线路',
                log.info("几何超限");
                switch (tableType){
                    case 1:  //    zhandianxinxi(1,"站点信息监测表"),
                        if(tplDetailType == TplPersonTypeEnum.zhedanghaopai.getCode()){
                            re = TplWordEnum.zaizhiliang6;// 站点信息监测表遮挡号牌
                        }else {
                            re = TplWordEnum.zaizhiliang5;// 站点信息监测表
                        }
                        break;
                    case 2:      //    liandengji(2,"立案登记表"),
                        if(geren){ // 个人
                            re = TplWordEnum.zaizhiliang3;
                        }else {
                            re = TplWordEnum.zaizhiliang4;  // 企业
                        }
                        break;
                    case 3:   //    chuliyijian(3,"案件处理意见"),
                        if(geren){
                            re = TplWordEnum.zaizhiliang1;
                        }else {
                            re = TplWordEnum.zaizhiliang2;
                        }
                        break;
                    case 4:     //    xunwenbilu(4,"询问笔录"),
                        if(tplDetailType == TplPersonTypeEnum.chezhufaren.getCode()) re = TplWordEnum.zaizhiliang10;
                        if(tplDetailType == TplPersonTypeEnum.daliren.getCode()) re = TplWordEnum.zaizhiliang09;
                        if(tplDetailType == TplPersonTypeEnum.jiahsiyuan.getCode()) re = TplWordEnum.zaizhiliang11;
                        break;
                    case 5:     //    weifaxingwei(5,"违法行为通知"),
                        re = TplWordEnum.zaizhiliang12;
                        break;
                    case 6:   //    songdahuizheng(6,"送达回证"),
                        re = TplWordEnum.zaizhiliang13;
                        break;
                    case 7:   //    chenshu(7,"陈述申辩"),
                        re = TplWordEnum.zaizhiliang14;
                        break;
                    case 8:   //    jieanbaogao(8,"结案报告"),
                        if(geren) re = TplWordEnum.zaizhiliang7;
                        if(!geren) re = TplWordEnum.zaizhiliang8;
                        break;
                    default:
                        break;
                }
                break;
            case 3 :   // 案件类型 1载质量2几何3年检4动态监控5改装6.环保7线路',
                log.info("年检");
                break;
            case 4 :   // 案件类型 1载质量2几何3年检4动态监控5改装6.环保7线路',
                log.info("动态监控");
                break;
            case 5 :   // 案件类型 1载质量2几何3年检4动态监控5改装6.环保7线路',
                log.info("改装");
                break;
            case 6 :   // 案件类型 1载质量2几何3年检4动态监控5改装6.环保7线路',
                log.info("环保");
                break;
            case 7 :   // 案件类型 1载质量2几何3年检4动态监控5改装6.环保7线路',
                log.info("路线");
                break;
            default:
                break;
        }

        //  返回 模板信息
        Integer templateId = re.getCode();
        String templateName = re.getLabel();
        reMap.put("templateId",templateId);
        reMap.put("templateName",templateName);
        return reMap;
        // end
    }

    //
}